<?php

namespace app\admin\model;

use think\Model;

/**
 * Store
 */
class Store extends Model
{
    // 表名
    protected $name = 'store';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 追加属性
    protected $append = [
        'province_id_text',
        // 'city_id_text',
        // 'district_id_text',
    ];




    public function setProvinceIdAttr($value): string
    {
        return is_array($value) && isset($value[0]) ? $value[0] : $value;
    }


    public function getProvinceIdAttr($value, $row)
    {
        $provinceId = [];
        $provinceId[] = $row['province_id'];
        $provinceId[] = $row['city_id'];
        $provinceId[] = $row['district_id'];
        return $provinceId;
    }
    public function getProvinceIdTextAttr($value, $row): string
    {
        $provinceId = [];

        if (isset($row['province_id']) && $row['province_id']) {
            $provinceId[] = $row['province_id'];
        }
        if (isset($row['city_id']) && $row['city_id']) {
            $provinceId[] = $row['city_id'];
        }
        if (isset($row['district_id']) && $row['district_id']) {
            $provinceId[] = $row['district_id'];
        }
        if (empty($provinceId)) {
            return '';
        }

        $cityNames = \think\facade\Db::name('area')->whereIn('id', $provinceId)->column('name');
        return $cityNames ? implode('-', $cityNames) : '';
    }


    public function setCityIdAttr($value): string
    {
        $provinceId = request()->param('province_id/a', []);
        return is_array($provinceId) && isset($provinceId[1]) ? $provinceId[1] : 0;
    }

    // public function getCityIdTextAttr($value, $row): string
    // {
    //     if ($row['city_id'] === '' || $row['city_id'] === null) return '';
    //     $cityNames = \think\facade\Db::name('area')->where('id', $row['city_id'])->column('name');
    //     return $cityNames ? implode(',', $cityNames) : '';
    // }


    public function setDistrictIdAttr($value): string
    {
        $provinceId = request()->param('province_id/a', []);
        return is_array($provinceId) && isset($provinceId[2]) ? $provinceId[2] : 0;
    }

    // public function getDistrictIdTextAttr($value, $row): string
    // {
    //     if ($row['district_id'] === '' || $row['district_id'] === null) return '';
    //     $cityNames = \think\facade\Db::name('area')->where('id', $row['district_id'])->column('name');
    //     return $cityNames ? implode(',', $cityNames) : '';
    // }

    public function getLongitudeAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getLatitudeAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getFloorAreaAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }
}
